package com.gsafety.libraryktx.widget

import android.content.Context
import android.util.AttributeSet
import android.widget.Toast
import com.gsafety.libraryktx.bean.AttachBean
import com.gsafety.libraryktx.util.GlideUtils
import com.lxj.xpopup.photoview.PhotoView
import java.io.File

/**
 * 图片下载ImageVIew
 */
class AttachImageView : PhotoView, IAttachDownloadView {

    private var belongId: String = ""
    var mIndex: Int = 0

    constructor(context: Context) : super(context) {
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
    }

    init {
        initView()
    }

    private fun initView() {
//        presenter = AttachDownloadPresenter(this)
    }

    fun setBelongId(belongId: String) {
        if (belongId.isNullOrEmpty()) {
            return
        }
        this.belongId = belongId
//        presenter.getAttachId(belongId)
    }

    /**
     * 超载车辆图片
     */
    fun getVehicleImg(vehiclePath: String) {
//        presenter.showTip = false
//        presenter.downVehicleImg(vehiclePath)
    }

    fun getImg(attachId: String, type: String) {
        val file = File(attachId)
        if (file.exists()) {
            doDownloadResult(attachId)
        } else {
//            presenter.showTip = false
//            presenter.downloadAttach(attachId, type)
        }
    }

    fun showDia(msg: String) {
        onError(msg)
    }

    fun disDia() {
    }

    override fun doAttachIdListResult(data: List<AttachBean>?) {
    }

    override fun doDownloadResult(path: String) {
        GlideUtils.getInstance()?.displayImage(path, this)
    }

    fun onError(error: String) {
        Toast.makeText(context,error,Toast.LENGTH_SHORT).show()
    }
}